<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:jsf="http://xmlns.jcp.org/jsf">
    <h:head>
    </h:head>
    <h:body>
        <f:metadata>
            <f:viewParam name="dataverseId" value="#{manageTemplatesPage.dataverseId}"/>
            <f:viewAction action="#{dataverseSession.updateLocaleInViewRoot}"/>
            <f:viewAction action="#{manageTemplatesPage.init}"/>
            <f:viewAction action="#{dataverseHeaderFragment.initBreadcrumbs(manageTemplatesPage.dataverse, bundle['dataset.manageTemplates.pageTitle'])}"/>
        </f:metadata>
        <ui:composition template="/dataverse_template.xhtml">
            <ui:param name="pageTitle" value="#{bundle['dataset.manageTemplates.pageTitle']} - #{manageTemplatesPage.dataverse.name}"/>
            <ui:param name="dataverse" value="#{manageTemplatesPage.dataverse}"/>
            <ui:param name="showMessagePanel" value="#{true}"/>
            <ui:define name="body">
                <h:form id="manageTemplatesForm">
                    <div class="form-group clearfix">
                        <ui:fragment rendered="#{manageTemplatesPage.dataverse.owner ne null and manageTemplatesPage.inheritTemplatesAllowed}">
                            <label for="templateRoot" class="normal-text">
                                <h:selectBooleanCheckbox id="templateRoot" value="#{manageTemplatesPage.inheritTemplatesValue}">
                                    <p:ajax update="@form" listener="#{manageTemplatesPage.updateTemplatesRoot}" />
                                </h:selectBooleanCheckbox>
                                <h:outputFormat value="#{bundle['dataset.manageTemplates.select.txt']}">
                                    <f:param value="#{manageTemplatesPage.dataverse.templateRootDataverseName}"/>
                                </h:outputFormat>
                            </label>
                        </ui:fragment>

                        <div class="pull-right">
                            <h:outputLink styleClass="btn btn-default"
                                          value="/template.xhtml?ownerId=#{manageTemplatesPage.dataverse.id}">
                                <span class="glyphicon glyphicon-plus"/> <h:outputText value="#{bundle['dataset.manageTemplates.createBtn']}"/>
                            </h:outputLink>
                        </div>
                    </div>
                    <div>
                        <div id="no-templates-message" class="panel panel-default" jsf:rendered="#{empty manageTemplatesPage.templates}">
                            <div class="panel-body">
                                <span class="help-block h3">#{bundle['dataset.manageTemplates.noTemplates.why.header']}</span>
                                <ul>
                                    <li><h:outputText value="#{bundle['dataset.manageTemplates.noTemplates.why.reason1']}" escape="false"/></li>
                                    <li><h:outputText value="#{bundle['dataset.manageTemplates.noTemplates.why.reason2']}" escape="false"/></li>
                                </ul>
                                <span class="help-block h3">#{bundle['dataset.manageTemplates.noTemplates.how.header']}</span>
                                <ul>
                                    <li><h:outputText value="#{bundle['dataset.manageTemplates.noTemplates.how.tip1']}" escape="false"/></li>
                                    <li>
                                        <h:outputFormat value="#{bundle['dataset.manageTemplates.noTemplates.how.tip2']}" escape="false">
                                            <f:param value="#{manageTemplatesPage.dataverse.alias}"/>
                                        </h:outputFormat>
                                    </li>
                                </ul>
                                <p>
                                    <h:outputFormat value="#{bundle['dataset.manageTemplates.noTemplates.getStarted']}" escape="false">
                                        <f:param value="#{systemConfig.guidesBaseUrl}"/>
                                        <f:param value="#{systemConfig.guidesVersion}"/>
                                    </h:outputFormat>
                                </p>
                            </div>
                        </div>
                        <p:dataTable id="allTemplates" styleClass="manageTable" var="template" value="#{manageTemplatesPage.templates}" widgetVar="allTemplates"
                                     rendered="#{!empty manageTemplatesPage.templates}">
                            <p:column width="30%" sortBy="#{template.name}" headerText="#{bundle['dataset.manageTemplates.tab.header.templte']}">
                                <h:outputText value="#{template.name}" />
                            </p:column>
                            <p:column width="17%" class="text-center" sortBy="#{template.createDate}" headerText="#{bundle['dataset.manageTemplates.tab.header.date']}">
                                <h:outputText value="#{template.createDate}" />
                            </p:column>
                            <p:column width="13%" class="text-center" sortBy="#{template.usageCount}" headerText="#{bundle['dataset.manageTemplates.tab.header.usage']}">
                                <h:outputText value="#{template.usageCount}" />
                            </p:column>
                            <p:column width="40%" class="col-manage-action text-center" headerText="#{bundle['dataset.manageTemplates.tab.header.action']}">
                                <div jsf:rendered="#{manageTemplatesPage.dataverse.id != template.dataverse.id}">
                                    <em class="text-muted">
                                        <h:outputFormat value="#{bundle['dataset.manageTemplates.tab.action.noedit.createdin']}" escape="false">
                                            <f:param value="#{template.dataverse.name}"/>
                                        </h:outputFormat>
                                    </em>
                                </div>
                                <p:commandLink styleClass="btn btn-default"
                                               value="#{bundle['dataset.manageTemplates.tab.action.btn.makeDefault']}"
                                               rendered="#{manageTemplatesPage.dataverse.defaultTemplate != template}"
                                               actionListener="#{manageTemplatesPage.makeDefault(template)}"
                                               update="@form,:messagePanel"/>
                                <p:commandLink styleClass="btn btn-default"
                                               value="#{bundle['dataset.manageTemplates.tab.action.btn.default']}"
                                               rendered="#{manageTemplatesPage.dataverse.defaultTemplate == template}"
                                               actionListener="#{manageTemplatesPage.unselectDefault(template)}"
                                               update="@form,:messagePanel">
                                    <span class="glyphicon glyphicon-eye-open"></span>
                                </p:commandLink>
                                <div class="btn-group" role="group">
                                    <p:commandLink styleClass="btn btn-default bootstrap-button-tooltip"
                                                   title="#{bundle['dataset.manageTemplates.tab.action.btn.view']}"
                                                   action="#{manageTemplatesPage.viewSelectedTemplate(template)}"
                                                   oncomplete="PF('viewTemplate').show();"
                                                   update=":manageTemplatesForm">
                                        <span class="glyphicon glyphicon-eye-open"></span>
                                    </p:commandLink>
                                    <p:commandLink styleClass="btn btn-default bootstrap-button-tooltip"
                                                   title="#{bundle['dataset.manageTemplates.tab.action.btn.copy']}"
                                                   action="#{manageTemplatesPage.cloneTemplate(template)}"
                                                   update="@form">
                                        <span class="glyphicon glyphicon-duplicate"></span>
                                    </p:commandLink>
                                    <div class="btn-group" jsf:rendered="#{manageTemplatesPage.dataverse.id eq template.dataverse.id}">
                                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
                                            <span class="glyphicon glyphicon-pencil"/> #{bundle['dataset.manageTemplates.tab.action.btn.edit']} <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li>
                                                <h:outputLink value="/template.xhtml?id=#{template.id}&#38;ownerId=#{manageTemplatesPage.dataverse.id}&#38;editMode=METADATA">
                                                    <h:outputText value="#{bundle['dataset.manageTemplates.tab.action.btn.edit.metadata']}" />
                                                </h:outputLink>
                                            </li>
                                            <li>
                                                <h:outputLink value="/template.xhtml?id=#{template.id}&#38;ownerId=#{manageTemplatesPage.dataverse.id}&#38;editMode=LICENSE">
                                                    <h:outputText value="#{bundle['dataset.manageTemplates.tab.action.btn.edit.terms']}" />
                                                </h:outputLink>
                                            </li>
                                        </ul>
                                    </div>
                                    <p:commandLink styleClass="btn btn-default bootstrap-button-tooltip"
                                                   title="#{bundle['dataset.manageTemplates.tab.action.btn.delete']}"
                                                   update="@form,:messagePanel"
                                                   action="#{manageTemplatesPage.setSelectedTemplate(template)}"
                                                   rendered="#{manageTemplatesPage.dataverse.id eq template.dataverse.id}"
                                                   oncomplete="PF('deleteConfirmation').show();">
                                        <span class="glyphicon glyphicon-trash"></span>
                                    </p:commandLink>
                                </div>
                            </p:column>
                        </p:dataTable>
                    </div>
                    <p:dialog id="deleteTemplateConfirm" header="#{bundle['dataset.manageTemplates.tab.action.btn.delete.dialog.header']}" 
                              styleClass="largePopUp" widgetVar="deleteConfirmation" modal="true" focus="contDeleteTemplateBtn">
                        <p class="help-block">
                            <span class="text-warning"><span class="glyphicon glyphicon-warning-sign"/> &#160;<h:outputText value="#{bundle['dataset.manageTemplates.tab.action.btn.delete.dialog.tip']}" /></span>
                        </p>
                        <ui:fragment rendered="#{manageTemplatesPage.selectedTemplate.isDefaultForDataverse}">
                            <p>
                                <h:outputText value="#{bundle['dataset.manageTemplates.delete.usedAsDefault']}" />
                            </p>
                            <ul>
                                <ui:repeat value="#{manageTemplatesPage.selectedTemplate.dataversesHasAsDefault}" var="dv">
                                    <li>
                                        <h:outputText value="#{dv.displayName}"/>
                                    </li>
                                </ui:repeat>
                            </ul>
                        </ui:fragment>
                        <div class="button-block">
                            <p:commandButton id="contDeleteTemplateBtn" styleClass="btn btn-default" value="#{bundle.continue}" onclick="PF('deleteConfirmation').hide()" action="#{manageTemplatesPage.deleteTemplate()}" update="@form,:messagePanel"/>
                            <button class="btn btn-link" onclick="PF('deleteConfirmation').hide();" type="button">
                                #{bundle.cancel}
                            </button>
                        </div>
                    </p:dialog>
                    <p:dialog id="viewTemplate" styleClass="largePopUp" header="#{bundle['dataset.manageTemplates.tab.action.btn.view.dialog.header']}" widgetVar="viewTemplate" modal="true">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label for="select_DatasetTemplate" class="col-sm-3 control-label">
                                    #{bundle['dataset.manageTemplates.tab.action.btn.view.dialog.datasetTemplate']}
                                </label>
                                <div class="col-sm-7">
                                    <p class="form-control-static">
                                        #{manageTemplatesPage.selectedTemplate.name}
                                    </p>
                                </div>
                            </div>
                        </div>
                        <ui:include src="metadataFragment.xhtml">
                            <ui:param name="managePage" value="true"/>
                            <ui:param name="metadataBlocks" value="#{TemplatePage.template.metadataBlocksForView.entrySet().toArray()}"/>
                            <ui:param name="template" value="#{TemplatePage.template}"/>
                        </ui:include>
                        <ui:include src="dataset-license-terms.xhtml">
                            <ui:param name="managePage" value="true"/>
                            <ui:param name="datasetPage" value="false"/>
                            <ui:param name="publicStore" value="settingsWrapper.publicInstall"/>
                            <ui:param name="termsOfUseAndAccess" value="#{TemplatePage.template.termsOfUseAndAccess}" />
                        </ui:include>
                        <div class="button-block">
                            <button class="btn btn-default" onclick="PF('viewTemplate').hide()" type="button">
                                #{bundle.close}
                            </button>
                        </div>
                    </p:dialog>
                </h:form>
            </ui:define>
        </ui:composition>
    </h:body>
</html>
